<?php



namespace Home\Model;

use Think\Model;



/**

 *查看学生的控制器

 */

class CourseRecordModel extends Model

{

	/**

	 * [studentOne description]查找要教的详细信息

	 * @param  [type] $uid [description]用户ID

	 * @param  [type] $cid [description]班级ID
	 
	 * @param  [type] $order [description]订单ID

	 * @return [type]      [description]

	 */

	public function studentOne($cid,$uid,$order){

		$map['m.cid'] = $cid;

		$map['m.uid'] = $uid;
		
		$map['m.order'] = $order;

		$map['c.status'] = 1;

		$result =D('Mycourse')

				->alias('m')

				->join('tutor_course as c on m.cid = c.id')

				->join('tutor_user_member as u on u.id = m.uid')

			    ->join('left join tutor_auth as a on u.id = a.uid')

				//->field('c.title,c.salary,m.uid,m.cancel,m.cid,u.nickname,u.age,u.sex,u.logo,u.id,u.students,u.area_id')
				//lkl修改，查询通过状态
				->field('c.passed,m.teachname as title,m.unit as salary,m.uid,m.cancel,m.cid,u.nickname,u.age,u.sex,u.logo,u.id,u.students,u.area_id,a.passed')

				->where($map)

				->find();

			$result['class_date'] = D('Mycourse') ->showTime($cid,$uid);
						/* yxf 20171107 时薪有几位小数就显示几位  S*/							$result['salary'] = floatval($result['salary']);								/* yxf 20171107 时薪有几位小数就显示几位 E */

		return $result;

	}

	/**

	 * [showTime description]查找全部的上课时间

	 * @param  [type] $uid [description]

	 * @param  [type] $cid [description]

	 * @return [type]      [description]

	 */

	public function showTime($uid,$cid,$order){

		$map['m.uid']   = $uid;

		$map['m.cid']   = $cid;

		$map['m.order'] = $order;

		$data = D('Mycourse')

				 ->alias('m')

				 ->join('tutor_order as o on m.uid = o.uid AND m.cid = o.cid AND m.order = o.order')

				 ->join('tutor_course_record as r on o.id = r.order_id')

				 ->field('r.*')

				 ->order('r.id')

				 ->where($map)

				 ->select();

		$num = $this ->where($map) ->count();

		$i = 1;

		foreach ($data as &$value) {

			$value['num'] = $i++;

		}

		return $data;

	}

		/**

	 * [showTimes description]查找全部的上课时间

	 * @param  [type] $uid [description]

	 * @param  [type] $cid [description]

	 * @return [type]      [description]

	 */

	public function showTimes($uid,$cid){

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$data = $this ->order('id') ->where($map) ->select();

		$num = $this ->where($map) ->count();

		$i = 1;

		foreach ($data as &$value) {

			$value['num'] = $i++;

		}

		return $data;

	}

	//退订的方法

	public function ajaxCancel($order){

		//如果没有上过课就删除

		$orderTow = $order;

		$mapOrder['r.at_class'] = 1;

		$mapOrder['o.order']    = $order;

		$model15  = D('Order') ->alias('o') ->join('tutor_course_record as r on o.id = r.order_id') ->where($mapOrder) ->find();

		if(!$model15){

			D('Mycourse') ->where("`order` = $order") ->save(array('is_del' =>1));

		}

		$order = D('Order') ->where(array("`order` = $order")) ->find();		

		$map['cancel']  = 1;

		//查询order_id

		$order_id = D('Order') ->where("`order` = $orderTow") ->find();

		//查询可以退订的课的条件

		$where['order_id'] = $order_id['id'];

		$where['at_class'] = 0;

		//查询可以退订几节课

		$courseNum = $this ->where($where) -> count();
		// $where['class_date'] = array('between',array('0',time()));//"class_date > 0 AND class_date < ".time();

		//改变我的课程改为退订状态

		$m=D('Home/Mycourse');

		$m->startTrans();//开启事物

		$data   = $m ->where(array('cid' =>$order['cid'],'uid' =>$order['uid'],'order' =>$order['order'])) ->save($map);

		// echo M()->_sql();

		//查找钱数

		$map1['order'] = $orderTow;

		$salaryArr = D('Order') ->field('salary') ->where($map1) ->find();

		$salary = $salaryArr['salary'];
		//需要退给学生的钱

		$stuSalary = $salary * $courseNum;

		//扣除平台的钱

		$root = D('Home/UserMember') ->lock(true) ->field('account') ->where(array('id' =>0)) ->find();

		$rootSalary = $root['account'] - $stuSalary;

		//更新系统的钱

		$updateRoot = D('Home/UserMember') ->where(array('id' =>0)) ->save(array('account' =>$rootSalary));



		//给学生加钱

		$stu = D('Home/UserMember') ->lock(true) ->field('account') ->where(array('id' =>$order['uid'])) ->find();

		$studentSalary = $stu['account'] + $stuSalary;

		//更新学生的钱

		$updateRoot1 = D('Home/UserMember') ->where(array('id' =>$order['uid'])) ->save(array('account' =>$studentSalary));

		$save['status'] = 3;

		$resylt = $this ->where(array('order_id' =>$order_id['id'] , 'at_class' =>0))->save($save);

		// echo M() -> _sql();die;

		$model1 = D('Home/Message') ->tuiding($order['uid'],$order['cid']);

		$model2 = D('Home/Account') ->tuiding($order['uid'],$order['cid'],$stuSalary);

		$model3 = D('Home/Sms') 	 ->tuiding($order['uid'],$order['cid']);

		//改状态

		if($data && $resylt && $courseNum && $updateRoot && $updateRoot1 && $model1 && $model2 && $model3 && $stu){

				$m->commit();//成功则提交

				return true;

		}else{

			// dump($data);

			// dump($resylt);

			// dump($courseNum);

			// dump($updateRoot);

			// dump($updateRoot1);

			// dump($model1);

			// dump($model2);

			// dump($model3);die;

			$m->rollback();//不成功，则回滚

			return false;

		}

	}



	/**

	 * 查找上课三天后还未确认的课时

	 * @return [array] [课时id]

	 */

	public function endClass(){
		//lkl修改，改为3天自动确认
		//$time =	time()-3600*24;
		$time =	time()-3600*24*3;

		$where['r.class_date'] = array('between', "1,$time");

		$where['r.at_class']	 = 0;

		$where['r.status']	 = 1;

		return $this ->alias('r') -> field('r.id,o.order') ->join('tutor_order as o on o.id = r.order_id') -> where($where) -> select();

	}

	/**

	 * 查找所有的时间

	 */

	public function showTimeAll($uid,$cid){

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$map['status'] = 1;

		$data = $this ->where($map) ->order('id') ->select();

		$i = 1;

		foreach ($data as &$value) {

			$value['num'] = $i++;

		}

		return $data;

	}

	/**

	 * 查找剩余的课时

	 */

	public function surplus($uid,$cid,$order){

		$order = D('Order') ->field('id') ->where(array('order' =>$order)) ->find();

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$map['order_id'] = $order['id'];

		$map['status']  = 1;

		$map['class_date'] = array('gt',time());

		return $this ->where($map) ->count();

	}

	/**

	 * 查找下一节课的上课时间

	 */

	public function nextTime($uid,$cid){

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$map['status'] = 1;

		$map['class_date'] = array('gt',time());

		return $this ->where($map) ->order('id') ->find();

	}

	/**

	 * 查找所有课程是否都确认

	 */

	public function affAll($uid,$cid,$order){

		$order = D('Order') ->field('id') ->where(array('order' =>$order)) ->find();

		$map['uid'] = $uid;

		$map['cid'] = $cid;

		$map['order_id'] = $order['id'];

		$map['at_class']  = 0;

		return $this ->where($map) ->find();

	}

	/**

	 * 判断是否上完全部课程

	 */

	public function class_ok($uid,$cid,$order){

		$map['o.cid'] = $cid;

		$map['o.uid'] = $uid;

		$map['o.order'] = $order;

		$map['r.status'] = 1;

		$map['r.at_class'] = 0;

		return D('Order')

				   ->alias('o')

				   ->join('tutor_course_record as r on r.order_id = o.id')

				   ->field('r.*')

				   ->where($map)

				   ->find();

	}

	/**

	 * 给学生发上课消息

	 */

	public function stuMsg($id){

		$data = $this ->field('uid') ->where(array('id' => $id)) ->find();

		$array = array(

			'sid'      => 0,

			'rid'	   => $data['uid'],

			'content'  => "您收到了新的课程安排，请在我的课程中查看。若您不能按时上课，请您和老师协商修改时间，感谢您的支持。",

			'time'     => time(),

			'type'     => 4

			);

		D('Message') ->add($array);

	}

	/**

	 * 查看确认上课的模型

	 */

	public function quanren($order){

		$map['o.order'] = $order;

		$map['r.at_class'] = 0;

		$map['r.class_date'] = array('between',array(1,time()));

		$data =  D('Order')

			     ->alias('o')

			     ->join('tutor_course_record as r on o.id = r.order_id')

			     ->where($map)

			     ->count();
			    
		return $data;

	}

	/**

	 * 

	 */

	public function orderTime($order){

		$map['o.order'] = $order;

	    $map['r.class_date'] = array('between',array(1,time()));

	    // $map['r.class_date'] = array('gt',0);

	    $map['r.status']     = 1;

	    $map['r.at_class']   = 0;

	    $result = D('Order')

	    		->alias('o')

	    		->join('tutor_course_record as r on o.id = r.order_id')

	    		->where($map)

	    		->select();

	    return $result;

	}

	public function showTimeOrder($order){

		$map['o.order'] = $order;

	    $map['r.status']     = 1;

	    $map['r.at_class']   = 1;

	    $result = D('Order')

	    		->alias('o')

	    		->join('tutor_course_record as r on o.id = r.order_id')

	    		->where($map)

	    		->select();

	    return $result;

	}



	//检测是否有未上的课程

	public function is_complete($uid='',$cid=''){

		if($uid != '') {

			//提现用户id

			$where['uid'] = $uid;

		}

		if ($cid != '') {

			//课程id

			$where['cid'] = $cid;

		}

		//未上课

		$where['at_class'] = 0;

		//未退订

		$where['status'] = 1;

		return $this -> where($where) -> select();

	}			/**	 * 判断是否上完全部课程	 */	public function class_no($uid,$cid,$order){		$map['o.cid'] = $cid;		$map['o.uid'] = $uid;		$map['o.order'] = $order;		$map['r.status'] = 1;		$map['r.at_class'] = 1;		return D('Order')				   ->alias('o')				   ->join('tutor_course_record as r on r.order_id = o.id')				   ->field('r.*')				   ->where($map)				   ->find();	}

}